﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CrystalDecisions.CrystalReports.Engine;

namespace Project_Udrinkwater.PagesAdmin
{
    public partial class OrderBill : System.Web.UI.Page
    {
        private string Connection = ConfigurationManager.ConnectionStrings["DB_Udrink"].ConnectionString;
        public string ConvestToDateN(string s)
        {
            string day = "";
            string mount = "";
            string years = "";

            try
            {
                string[] stemp = s.Split('/');
                mount = (stemp[0]);
                day = (stemp[1]);
                years = (stemp[2]);
                if (Convert.ToInt32(years) < 2557)
                {
                    years = (Convert.ToInt32(years) + 543).ToString();
                }

                return (day + "/" + mount + "/" + years);
            }
            catch (Exception)
            {

            }
            return (day + "/" + mount + "/" + years);
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            
            string Address = Session["Address"].ToString();
            //string Distric=Session["Distric"].ToString();
            //string Amphur= Session["Amphur"].ToString();
            //string Province=Session["Province"].ToString();
            //string Postcode=Session["Postcode"].ToString();
            //string Phone=Session["Phone"].ToString();
            //string Fax=Session["Fax"].ToString();
            string a =Session["DocDate"].ToString();
            DateTime docDate = Convert.ToDateTime(a);
            string DocDate = ConvestToDateN((docDate.Date).ToShortDateString());
            
            string b = Session["DeeadLine"].ToString();
            DateTime DeadLine = Convert.ToDateTime(a);
            string DeeadLine=ConvestToDateN((DeadLine.Date).ToShortDateString());
            string PrintDate = ConvestToDateN((DateTime.Now).ToShortDateString());

            string DocNo=Session["DocNo"].ToString();
            string Employee=Session["Employee"].ToString();
            string Customer=Session["Customer"].ToString();
            string OrderId = Session["Order_Id"].ToString();
                
                SqlConnection objConn = new SqlConnection();
                SqlCommand objCmd = new SqlCommand();
                SqlDataAdapter dtAdapter = new SqlDataAdapter();

                DataSet ds = new DataSet();
                DataTable dt = null;
                string strConnString = null;
                StringBuilder strSQL = new StringBuilder();
                strSQL.Append(" SELECT TB_Order.Order_ID, TB_Order_List.id,ROW_NUMBER() OVER(ORDER BY TB_Order.Order_ID DESC) AS Row, TB_Order_List.Order_Category_ID, TB_Order_List.Product_ID, TB_Order_List.Price_Unit,");
                strSQL.Append(" TB_Order_List.Amount,TB_Order_List.Discount,TB_Order_Category.Order_Category_Name,TB_Product.Product_Name FROM TB_Order");               
                strSQL.Append(" LEFT JOIN TB_Order_List ON TB_Order.Order_ID = TB_Order_List.Order_ID");
                strSQL.Append(" LEFT JOIN TB_Order_Category ON TB_Order_List.Order_Category_ID = TB_Order_Category.Order_Category_ID");
                strSQL.Append(" LEFT JOIN TB_Product ON TB_Order_List.Product_ID = TB_Product.Product_ID AND TB_Order_Category.Product_ID=TB_Product.Product_ID");
                strSQL.Append(" WHERE TB_Order.Order_ID = "+OrderId+" ");
                
                

                objConn.ConnectionString = Connection;
                var _with1 = objCmd;
                _with1.Connection = objConn;
                _with1.CommandText = strSQL.ToString();
                _with1.CommandType = CommandType.Text;
                dtAdapter.SelectCommand = objCmd;

                dtAdapter.Fill(ds, "CrystalReport1");
                dt = ds.Tables[0];

                dtAdapter = null;
                objConn.Close();
                objConn = null;

                ReportDocument rpt = new ReportDocument();
                rpt.Load(Server.MapPath("~/Empoyee_Management/CrystalReport1.rpt"));

                rpt.SetDataSource(dt);
                rpt.SetParameterValue("DocNo",DocNo );
                rpt.SetParameterValue("DateDoc",DocDate );
                rpt.SetParameterValue("Adress",Address);
                //rpt.SetParameterValue("District", Distric);
                //rpt.SetParameterValue("AmPhur",Amphur);
                //rpt.SetParameterValue("Province",Province);
                //rpt.SetParameterValue("PostCode",Postcode);
                //rpt.SetParameterValue("Phone",Phone);
                //rpt.SetParameterValue("Fax",Fax);
                 rpt.SetParameterValue("DateP",PrintDate);
                rpt.SetParameterValue("DeadLine",DeeadLine);
                rpt.SetParameterValue("Employee",Employee);
                rpt.SetParameterValue("Customer",Customer);
                this.CrystalReportViewer2.ReportSource = rpt;


            
        }
    }
}